#include <QDebug>
#include "tabbedcontainer.h"
#include <QBoxLayout>
#include <QTableWidget>
#include "splitter.h"


TabbedContainer::TabbedContainer(QWidget *parent):IndexedContainer(parent)
{
    m_tabwidget = new QTabWidget(this);
    QBoxLayout* layout = new QHBoxLayout();
    layout->setMargin(0);
    layout->addWidget(m_tabwidget);
    this->setLayout(layout);
}


void TabbedContainer::insertWidget(int index, QWidget *widget)
{
    m_tabwidget->insertTab(index, widget, QString());

    //does not paint correctly without this call
    this->adjustSize();
}

void TabbedContainer::removeWidget(QWidget *widget)
{
//    m_tabwidget->removeTab(indexOf(widget));
    widget->setParent(0);
    if (count() == 1){//the splitter is now pointless
        QDockItem* parent = parentDockItem();
        QDockItemContainer* container = qobject_cast<QDockItemContainer*>(parent);
        if (container){
            container->replaceWidget(this,this->widgetAt(0));
        }
//        QBaseDock
        this->destroy();
    }
}

int TabbedContainer::indexOf(QWidget *widget)
{
    return m_tabwidget->indexOf(widget);
}

QWidget *TabbedContainer::widgetAt(int index)
{
    return m_tabwidget->widget(index);
}

int TabbedContainer::dockingIndex(DockOption dockoption, QWidget *relativeAnchor)
{

}

void TabbedContainer::setCurrentIndex(int index)
{
    m_tabwidget->setCurrentIndex(index);
}
